using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Data.Linq;
using DataObject.Core;
using DataObject.Database;
using DataObject.Model;

namespace DataObject.DataAccess
{
    public partial class PaymentBatchDAO : DataBaseProcessBase<PaymentBatch, OBSDataContext>
    {
        public static PaymentBatchDAO CreateInstant()
        {
            return new PaymentBatchDAO();
        }

        public IList<PaymentBatchDTO> GetPaymentList(Guid reservaiontID)
        {
            IList<PaymentBatchDTO> list = null;
            using (var db = new OBSDataContext(base.connectionString))
            {
                db.DeferredLoadingEnabled = false;
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<Reservation>(b => b.ReservationPayments);
                dlo.LoadWith<ReservationPayment>(b => b.PaymentBatch);
                dlo.LoadWith<PaymentBatch>(b => b.PaymentBatchDetails);
                db.LoadOptions = dlo;

                var query = db.Reservations.SingleOrDefault(x => x.ReservationID == reservaiontID);
                if (query != null)
                {
                    list = new List<PaymentBatchDTO>();
                    if (query.ReservationPayments != null && query.ReservationPayments.Count > 0)
                    {
                        foreach (var item in query.ReservationPayments)
                        {
                            if (item.PaymentBatch != null)
                            {
                                var paymentBatch = PaymentBatchMapper.ToBusinessObjectHasChildren(item.PaymentBatch, typeof(PaymentBatchDetail));
                                paymentBatch.DepositFlag = item.IsDeposit;
                                list.Add(paymentBatch);
                            }
                        }
                    }
                }

            }
            return list;
        }

        #region Extensibility Method Declarations
        public void InsertCollection(List<PaymentBatch> ls)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                db.PaymentBatches.InsertAllOnSubmit(ls);
                db.SubmitChanges();
            }
        }
        public void Update(PaymentBatchDTO item)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                var entity = db.PaymentBatches.SingleOrDefault(x => x.PaymentBatchID == item.PaymentBatchID);
                if (entity != null)
                {
                    entity = PaymentBatchMapper.ToEntity(entity, item);
                    db.SubmitChanges();
                }
            }
        }
        public List<PaymentBatch> GetAll()
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.PaymentBatches.ToList();
            }
        }
        public PaymentBatch GetByID(Guid paymentBatchID)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.PaymentBatches.SingleOrDefault(x => x.PaymentBatchID.Equals(paymentBatchID));
            }
        }
        #endregion
    }
}
